// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.properties.controllers;

import org.dengues.core.components.NodePropertyParameter;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.designer.ui.properties.PropertyTabDynamicSection;
import org.dengues.designer.ui.properties.cmd.PropertyChangeTextCommand;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Text;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * @since 2007-10-10 下午09:40:30
 */
public class FilePropertyController extends AbstractPropertyController {

    /**
     * Qiang.Zhang.Adolf@gmail.com FilePropertyController constructor comment.
     * 
     * @param dps
     */
    public FilePropertyController(PropertyTabDynamicSection dps) {
        super(dps);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.properties.controllers.AbstractPropertyController#createControl(org.eclipse.swt.widgets.Composite,
     * org.dengues.core.components.NodePropertyParameter, org.eclipse.swt.widgets.Control)
     */
    @Override
    public Control createControl(final Composite composite, final NodePropertyParameter param, Control lastControl) {
        Composite subcomposite = new Composite(composite, SWT.NONE);
        GridLayout gridLayout = new GridLayout(3, false);
        gridLayout.marginBottom = 0;
        gridLayout.marginHeight = 0;
        gridLayout.marginLeft = 0;
        gridLayout.marginRight = 0;
        gridLayout.marginTop = 0;
        gridLayout.marginWidth = 0;
        subcomposite.setLayout(gridLayout);
        subcomposite.setLayoutData(new GridData(GridData.FILL_BOTH));
        subcomposite.setBackground(composite.getBackground());
        CLabel cLabel = getWidgetFactory().createCLabel(subcomposite, param.getDisplayName());
        GridData gridData = new GridData();
        gridData.widthHint = STANDARD_LABEL;
        cLabel.setLayoutData(gridData);
        Text text = getWidgetFactory().createText(subcomposite, "", SWT.BORDER); //$NON-NLS-1$
        ControlDecoration controlDecoration = new ControlDecoration(text, SWT.RIGHT | SWT.TOP);
        if (param.isRequired()) {
            FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(
                    FieldDecorationRegistry.DEC_REQUIRED);
            controlDecoration.setImage(fieldDecoration.getImage());
            controlDecoration.setDescriptionText(fieldDecoration.getDescription());
        }
        final Text labelText = (Text) controlDecoration.getControl();

        register(param, labelText);

        if (param.getValue() != null) {
            labelText.setText(param.getValue().toString());
        }
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        data.heightHint = SWT.DEFAULT;
        data.horizontalIndent = FieldDecorationRegistry.getDefault().getMaximumDecorationWidth();
        labelText.setLayoutData(data);
        labelText.setData(PARAMETER_NAME, param.getName());
        labelText.setEditable(!param.isReadOnly());
        Button browse = getWidgetFactory().createButton(subcomposite,
                Messages.getString("FilePropertyController.browse"), SWT.PUSH); //$NON-NLS-1$
        browse.addSelectionListener(new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                String open = null;
                Path path = null;
                FileDialog dialog = new FileDialog(composite.getShell());
                path = new Path(param.getValue().toString());
                dialog.setFileName(path.toOSString());
                open = dialog.open();
                if (open != null) {
                    path = new Path(open);
                    PropertyChangeTextCommand command = new PropertyChangeTextCommand(dps.getElem(), param.getName(), path
                            .toPortableString());
                    getCommandStack().execute(command);
                    labelText.setText(path.toPortableString());
                }
            }
        });
        return browse;
    }

}
